import { Injectable, LoggerService as NestLoggerService } from '@nestjs/common';
import { Logger } from 'winston';
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
import { Inject } from '@nestjs/common';

@Injectable()
export class LoggerService implements NestLoggerService {
  private context?: string;

  constructor(
    @Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger,
  ) {}

  /**
   * 设置日志上下文
   * @param context 上下文名称，通常是类名或模块名
   */
  setContext(context: string) {
    this.context = context;
  }

  /**
   * 记录日志信息
   * @param message 日志消息
   * @param context 可选上下文，如果不提供则使用默认上下文
   */
  log(message: any, context?: string) {
    context = context || this.context;

    if (typeof message === 'object') {
      const { message: msg, ...meta } = message;
      this.logger.info(`[${context}] ${msg}`, { context, ...meta });
    } else {
      this.logger.info(`[${context}] ${message}`, { context });
    }
  }

  /**
   * 记录错误日志
   * @param message 错误消息
   * @param trace 错误堆栈
   * @param context 可选上下文
   */
  error(message: any, trace?: string, context?: string) {
    context = context || this.context;

    if (typeof message === 'object') {
      const { message: msg, ...meta } = message;
      this.logger.error(`[${context}] ${msg}`, {
        context,
        trace: trace || (message instanceof Error ? message.stack : undefined),
        ...meta,
      });
    } else {
      this.logger.error(`[${context}] ${message}`, {
        context,
        trace,
      });
    }
  }

  /**
   * 记录警告日志
   * @param message 警告消息
   * @param context 可选上下文
   */
  warn(message: any, context?: string) {
    context = context || this.context;

    if (typeof message === 'object') {
      const { message: msg, ...meta } = message;
      this.logger.warn(`[${context}] ${msg}`, { context, ...meta });
    } else {
      this.logger.warn(`[${context}] ${message}`, { context });
    }
  }

  /**
   * 记录调试日志
   * @param message 调试消息
   * @param context 可选上下文
   */
  debug(message: any, context?: string) {
    context = context || this.context;

    if (typeof message === 'object') {
      const { message: msg, ...meta } = message;
      this.logger.debug(`[${context}] ${msg}`, { context, ...meta });
    } else {
      this.logger.debug(`[${context}] ${message}`, { context });
    }
  }

  /**
   * 记录详细日志
   * @param message 详细消息
   * @param context 可选上下文
   */
  verbose(message: any, context?: string) {
    context = context || this.context;

    if (typeof message === 'object') {
      const { message: msg, ...meta } = message;
      this.logger.http(`[${context}] ${msg}`, { context, ...meta });
    } else {
      this.logger.http(`[${context}] ${message}`, { context });
    }
  }

  /**
   * 记录HTTP请求日志
   * @param message HTTP消息
   * @param context 可选上下文
   */
  http(message: any, context?: string) {
    context = context || this.context;

    if (typeof message === 'object') {
      const { message: msg, ...meta } = message;
      this.logger.http(`[${context}] ${msg}`, { context, ...meta });
    } else {
      this.logger.http(`[${context}] ${message}`, { context });
    }
  }
}
